package nio.muban;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FIleNio {

    public static void main(String[] args) throws IOException {
        FileInputStream from = new FileInputStream("/tmp/a.txt");
        FileChannel fromChannel = from.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        FileOutputStream to = new FileOutputStream("/tmp/b.txt");
        FileChannel toChannel = to.getChannel();

        while(fromChannel.read(byteBuffer) != -1) {
            for (int i=0; i<byteBuffer.position(); i++) {
                System.out.print((char)byteBuffer.get(i));
            }
            System.out.println();
            byteBuffer.flip();
            toChannel.write(byteBuffer);
            byteBuffer.clear();
        }

        toChannel.close();
        fromChannel.close();
    }

}
